package com.example.tool.util.net;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;

import Decoder.BASE64Decoder;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * 描述:网络统一管理类
 *
 * @author: liangkun
 * @date: 2019/12/20 14:54
 */
public class RetrofitUtil {
    private static final int DEFAULT_TIMEOUT = 5;
    public ApiService apiService;

    private static RetrofitUtil instance = null;

    private RetrofitUtil() {

    }

    public static RetrofitUtil getInstance() {
        synchronized (RetrofitUtil.class) {
            if (instance == null) {
                instance = new RetrofitUtil();
            }
        }
        return instance;
    }
    /**
     * @param
     * @return
     * @description 请求方法
     * @author liangkun
     * @time 2019/12/12 10:58
     */
    public RetrofitUtil request(String baseUrl) {
//        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> DebugLog.i(DebugLog.KUN + "HttpLoggingInterceptor:" + message));
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        //手动创建一个OkHttpClient并设置超时时间,并打印请求和返回的日志消息
//        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS).addInterceptor(logging);
        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

        Retrofit retrofit = new Retrofit.Builder()
                .client(httpClientBuilder.build())
                .addConverterFactory(new NullOnEmptyConverterFactory())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(baseUrl)
                .build();

        //创建代理对象
        apiService = retrofit.create(ApiService.class);
        return this;
    }


}
